package com.xygz.orm;

import com.xiyuan.smartutils.Asserts;
import com.xiyuan.smartutils.ids.Ids;
import com.xygz.orm.table.ZTableField;
import com.xygz.orm.utils.LBDA;
import com.xygz.orm.utils.S;

/**
 * Select Column
 *
 * @author xiyuan-lgz 2023-09-26 @version v1.0.0 新建与整理
 */
@SuppressWarnings("all")
public class SColumn {
    private String id = Ids.uuid();
    private String column;
    private String as;
    
    private SFunc<?, ?> pfunc;
    
    private ZTableField field;
    
    /**
     * 默认是 查询表列
     */
    private Func qType = Func.DEF;
    
    public SColumn setField(ZTableField field) {
        this.field = field;
        return this;
    }
    
    public ZTableField getField() {
        return field;
    }
    
    public SColumn(String column) {
        Asserts.nonNull(column, "Column cannot be null");
        this.column = S.trimToNull(column);
    }
    
    public SColumn(SFunc<?, ?> column) {
        Asserts.nonNull(column, "Column cannot be null");
        this.pfunc = column;
    }
    
    public SColumn as(String column) {
        this.as = S.trimToNull(column);
        return this;
        
    }
    
    public SColumn as(SFunc<?, ?> column) {
        this.as = LBDA.getFieldName(column);
        return this;
    }
    
    public String column() {
        return column;
    }
    
    public String as() {
        return as;
    }
    
    public SFunc<?, ?> getPfunc() {
        return pfunc;
    }
    
    public Func getQType() {
        return qType;
    }
    
    public SColumn setQType(Func qType) {
        this.qType = qType;
        return this;
    }
    
    public String getId() {
        return id;
    }
    
    public SColumn setId(String id) {
        this.id = id;
        return this;
    }
    
    @Override
    public boolean equals(Object obj) {
        if(obj == null || (obj instanceof SColumn)) {return false;}
        return this == obj || this.id.equals(((SColumn)obj).id);
    }
    
    
    @Override
    public int hashCode() {
        return id.hashCode();
    }
    
    public static enum Func {
        
        /**
         * 默认 表列
         */
        DEF,
        
        /**
         * 平均
         */
        AVG,
        /**
         * 求和
         */
        SUM,
        /**
         * 最大值
         */
        MAX,
        /**
         * 最小值
         */
        MIN,
        /**
         * 计数
         */
        COUNT,
        
        //
        ;
        public SColumn col(String col) {
            return new SColumn(col).setQType(this);
        }
        public <E> SColumn col(SFunc<E, ?> column) {
            return new SColumn(column).setQType(this);
        }
    }
}
